package com.taysear.parcel;

import java.util.Date;

import com.taysear.parcel.domain.LoadingsheetHistory;
import com.taysear.parcel.domain.ParcelHistory;
import com.vaadin.addon.jpacontainer.JPAContainer;
import com.vaadin.ui.Component;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.themes.Runo;

@SuppressWarnings("serial")
public class LoadingsheetHistoryTable extends Table {

	
	public LoadingsheetHistoryTable() {
		super();
	}

	public LoadingsheetHistoryTable(final ParcelApplication app, JPAContainer<LoadingsheetHistory> container, String [] columns) {
		super(null, container);
		
		addGeneratedColumn("User", new ColumnGenerator() {
		  public Component generateCell(Table source, Object itemId,
		            Object columnId) {
		      if (getItem(itemId).getItemProperty("user").getValue() != null) {
		          Label l = new Label();
		          l.setValue(getItem(itemId).getItemProperty("user").getValue());
		          l.setSizeUndefined();
		          return l;
		      }
		      return null;
		  }
		});
		addGeneratedColumn("Action", new ColumnGenerator() {
		  public Component generateCell(Table source, Object itemId,
		            Object columnId) {
		      if (getItem(itemId).getItemProperty("loadingsheetAction").getValue() != null) {
		          Label l = new Label();
		          l.setValue(getItem(itemId).getItemProperty("loadingsheetAction").getValue());
		          l.setSizeUndefined();
		          return l;
		      }
		      return null;
		  }
		});
		addGeneratedColumn("Date", new ColumnGenerator() {
		  public Component generateCell(Table source, Object itemId,
		            Object columnId) {
		      if (getItem(itemId).getItemProperty("date").getValue() != null) {
		          Label l = new Label();
		          Date date = (Date) getItem(itemId).getItemProperty("date").getValue();
		          l.setValue(app.formatDate(date));
		          l.setSizeUndefined();
		          return l;
		      }
		      return null;
		  }
		});
		addGeneratedColumn("Description", new ColumnGenerator() {
		  public Component generateCell(Table source, Object itemId,
		            Object columnId) {
		      if (getItem(itemId).getItemProperty("description").getValue() != null) {
		          Label l = new Label();
		          l.setValue(getItem(itemId).getItemProperty("description").getValue());
		          l.setSizeUndefined();
		          return l;
		      }
		      return null;
		  }
		});
		addStyleName(Runo.TABLE_SMALL);
		setVisibleColumns(columns);
		setSizeUndefined();
		setImmediate(true);
		setPageLength(container.size());
	}
}